package com.shell.governor.util;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.ind.PubConstants;
import com.shell.framework.kernel.context.ContextCache;
import com.shell.governor.modules.po.PubDictionary;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class TranslateUtil {
	public static String getConstantName(String constantType, String constantValue) throws Exception {
		Map<String, Map<String, String>> constantsValueMap = SystemUtil.getConstantsValueMap(PubConstants.class);
		if ((constantsValueMap.containsKey(constantType)) && (constantsValueMap.get(constantType) != null)) {
			Map<String, String> vals = (Map) constantsValueMap.get(constantType);
			if (vals.containsKey(constantValue)) {
				return (String) vals.get(constantValue);
			}
		}
		return "";
	}

	public static String getDicName(String dicType, String dicValue) throws Exception {
		return getDicName(dicType, dicValue, "", "");
	}

	public static String getDicName(String dicType, String dicValue, String separator, String wraper) throws Exception {
		if (StringUtil.emptyAndNull(separator)) {
			separator = ",";
		}
		if (StringUtil.emptyAndNull(wraper)) {
			wraper = "";
		}
		Map<String, Object> context = ContextCache.getValue("CONTEXT_DATA_CACHE_DICTIONARY");
		if ((null != context) && (context.containsKey("dictionaryMap"))) {
			Map<String, Object> dics = (Map) context.get("dictionaryMap");
			if (dics.containsKey(dicType)) {
				PubDictionary dic = (PubDictionary) dics.get(dicType);

				return getDicText(separator, wraper, dic.getChildren(), dicValue);
			}
		}
		return "";
	}

	private static String getDicText(String separator, String wraper, List<PubDictionary> items, String dicValue) {
		if (!StringUtil.emptyAndNull(wraper)) {
			dicValue.replaceAll(wraper, "");
		}
		String[] codeArr = dicValue.split(separator);

		String text = "";
		for (int i = 0; i < codeArr.length; i++) {
			for (int j = 0; j < items.size(); j++) {
				if (((PubDictionary) items.get(j)).getDicCode().equalsIgnoreCase(codeArr[i])) {
					text = text + separator + ((PubDictionary) items.get(j)).getDicName();
				}
			}
		}
		if (!StringUtil.emptyAndNull(text)) {
			text = text.replace(separator, "");
		}
		return text;
	}

	public static boolean bigDecimalEqualToInt(BigDecimal a, int b) {
		if (null == a) {
			return false;
		}
		if (a.compareTo(new BigDecimal(b)) != 0) {
			return false;
		}
		return true;
	}
}
